import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import {ElMessage} from "element-plus";

const Home = () => import('@/views/Home.vue')
const HomeMain = () => import('@/views/home/main/HomeMain.vue')
const Article = () => import('@/views/home/article/Article.vue')
const ArticlePage = () => import('@/views/home/article/ArticlePage.vue')
const Community = () => import('@/views/home/community/Community.vue')
const Mall = () => import("@/views/home/mall/Mall.vue")
const Activity = () => import('@/views/home/activity/Activity.vue')

const Profile = () => import('@/views/Profile.vue')
const EditUser = () => import("@/views/profile/userManager/EditUser.vue")
const Login = () => import('@/views/Login.vue')
const Register = () => import("@/views/Register.vue")


const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: Home,
      redirect: "main",
      children: [
          {
              path: "/main",
              name: "HomeMain",
              component: HomeMain
          }, {
              path: "/article",
              name: "Article",
              component: Article
          },{
              path: "/community",
              name: "Community",
              component: Community
          },{
              path: "/articlePage/:id",
              name: "ArticlePage",
              component: ArticlePage,
              meta: {
                  requireAuth: true
              }
          },{
              path: "/mall",
              name: "Mall",
              component: Mall,
              meta: {
                  requireAuth: false
              }
          },{
              path: "/activity",
              name: "Activity",
              component: Activity,
              meta: {
                  requireAuth: false
              }
          }
      ]
  }, {
        path: '/profile',
        name: 'Profile',
        component: Profile,
        meta: {
            requireAuth: true
        }
    }, {
        path: '/editUser',
        name: 'EditUser',
        component: EditUser,
        meta: {
            requireAuth: true
        }
    },{
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      requireAuth: false
    }
  },{
    path: '/register',
    name: 'Register',
    component: Register,
    meta: {
      requireAuth: false
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
import {GETA} from '@/api/index'
import store from "@/store";

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    GETA("/Tag/GetTags", {}).then(res => {
        console.log(res)
        if (res.status === 200) {
            next()
        } else if (res.status === 403) {
            ElMessage.warning("您还未登录，请先登录")
        } else {
            ElMessage.error("请求出错啦！")
        }
    }).catch(err => {
        // store.commit("LOGOUT", null)
        ElMessage.warning("您还未登录，即将跳转到登录页面")
        setTimeout(() => {
            next({
                path: "/login",
                // query: {redirect: to.fullPath}
            })
        }, 1500)
    })
  } else {
    next()
  }
})

export default router
